const BaseService = require("./baseService");

class CategoryInfoService extends BaseService {
    constructor() {
        super(...arguments);
        this.tableName = "categoryinfo";
    }
    async getListByPage({
        categoryname,
        pageIndex
    } = {}) {
        pageIndex = pageIndex || 1;
        let strWhere = "";
        if (categoryname) {
            strWhere += " and categoryname like '%" + categoryname + "%'";
        }
        let strSql = `select * from ${this.tableName} where isDel=false ${strWhere} limit ?,? `;
        let countSql = this.createCountSql(strWhere);
        let result = await this.app.mysql.query(strSql + ";" + countSql, [(pageIndex - 1) * this.pageSize, this.pageSize]);
        return result;
    }
    async addCategoryInfo({
        categoryname
    }) {
        let result = await this.app.mysql.insert(this.tableName, {
            categoryname
        });
        return result.affectedRows > 0 ? true : false;
    }
    async findByCategoryId(categoryid) {
        let result = await this.app.mysql.get(this.tableName, {
            categoryid,
            isDel: false
        });
        return result;
    }
    async editCategoryInfo({
        categoryid,
        categoryname
    }) {
        let result = await this.app.mysql.update(this.tableName, {
            categoryname
        }, {
            where: {
                categoryid
            }
        });
        return result.affectedRows > 0 ? true : false;
    }
    /**
     * 
     * @param {Array} cksValue 删除的数组
     */
    async deleteCategoryList(cksValue) {
        let result = await this.app.mysql.update(this.tableName, {
            isDel: true
        }, {
            where: {
                categoryid: cksValue
            }
        });
        return result.affectedRows > 0 ? true : false;
    }
}

module.exports = CategoryInfoService;